<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="PLC编号" prop="dvcNo">
        <el-input
          v-model="queryParams.dvcNo"
          placeholder="请输入PLC编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width:140px;"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isok">
        <el-select v-model="queryParams.isok" placeholder="请选择状态" style="width:140px;" clearable size="small">
          <el-option
            v-for="dict in isokOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:WcsDvc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:WcsDvc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:WcsDvc:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:WcsDvc:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="WcsDvcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="dvcId"  /> -->
      <el-table-column label="PLC编号" align="center" prop="dvcNo"  />
      <el-table-column label="厂商名称" align="center" prop="dvcDesc" min-width="160" />
      <el-table-column label="接口程序名称" align="center" prop="dvcTp" min-width="160" :show-overflow-tooltip="true" />
      <el-table-column label="IP地址" align="center" prop="dvcIp" min-width="160" />
      <el-table-column label="报文头" align="center" prop="revBegin" min-width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="报文尾" align="center" prop="revEnd" min-width="160" :show-overflow-tooltip="true" />
      <el-table-column label="接收存储过程" align="center" prop="revProc" min-width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="发送存储过程" align="center" prop="sendProc" min-width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="发送间隔" align="center" prop="sendTime"  />
      <el-table-column label="端口号" align="center" prop="port"  />
      <!-- <el-table-column label="DVC_ST" align="center" prop="dvcSt"  /> -->
      <el-table-column label="状态" align="center" prop="isok" :formatter="isokFormat" />
      <!-- <el-table-column label="创建时间" align="center" prop="createDate" width="160">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>  -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改PLC接口参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="PLC编号" prop="dvcNo">
          <el-input v-model="form.dvcNo" placeholder="请输入PLC编号" />
        </el-form-item>
        <el-form-item label="厂商名称" prop="dvcDesc">
          <el-input v-model="form.dvcDesc" placeholder="请输入厂商名称" />
        </el-form-item>
        <el-form-item label="接口程序名称" prop="dvcTp">
          <el-input v-model="form.dvcTp" placeholder="请输入接口程序名称" />
        </el-form-item>
        <el-form-item label="IP地址" prop="dvcIp">
          <el-input v-model="form.dvcIp" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="报文头" prop="revBegin">
          <el-input v-model="form.revBegin" placeholder="请输入报文头" />
        </el-form-item>
        <el-form-item label="报文尾" prop="revEnd">
          <el-input v-model="form.revEnd" placeholder="请输入报文尾" />
        </el-form-item>
        <el-form-item label="接收存储过程" prop="revProc">
          <el-input v-model="form.revProc" placeholder="请输入接收存储过程" />
        </el-form-item>
        <el-form-item label="发送存储过程" prop="sendProc">
          <el-input v-model="form.sendProc" placeholder="请输入发送存储过程" />
        </el-form-item>
        <el-form-item label="发送间隔" prop="sendTime">
          <el-input v-model="form.sendTime" placeholder="请输入发送间隔" />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口号" />
        </el-form-item>
        <el-form-item label="状态" prop="isok">
          <el-select v-model="form.isok" placeholder="请选择状态">
            <el-option
              v-for="dict in isokOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWcsDvc, getWcsDvc, delWcsDvc, addWcsDvc, updateWcsDvc, exportWcsDvc } from "@/api/system/WcsDvc";

export default {
  name: "WcsDvc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // PLC接口参数表格数据
      WcsDvcList: [],
        // 日期范围
        dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // ID字典
      dvcIdOptions: [],
      // PLC编号字典
      dvcNoOptions: [],
      // 厂商名称字典
      dvcDescOptions: [],
      // 接口程序名称字典
      dvcTpOptions: [],
      // IP地址字典
      dvcIpOptions: [],
      // 报文头字典
      revBeginOptions: [],
      // 报文尾字典
      revEndOptions: [],
      // 接收存储过程字典
      revProcOptions: [],
      // 发送存储过程字典
      sendProcOptions: [],
      // 发送间隔字典
      sendTimeOptions: [],
      // 端口号字典
      portOptions: [],
      // DVC_ST字典
      dvcStOptions: [],
      // 创建时间字典
      createDateOptions: [],
      // 更新时间字典
      updateDateOptions: [],
      // 备注字典
      commentsOptions: [],
      // HEART_BEAT_ST字典
      heartBeatStOptions: [],
      // 状态字典
      isokOptions: [],
      // vanderland_port字典
      vanderlandPortOptions: [],
      // errorCode字典
      errorcodeOptions: [],
      // errRfidList字典
      errrfidlistOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dvcId: null,
        dvcNo: null,
        dvcDesc: null,
        dvcTp: null,
        dvcIp: null,
        revBegin: null,
        revEnd: null,
        revProc: null,
        sendProc: null,
        sendTime: null,
        port: null,
        dvcSt: null,
        createDate: null,
        updateDate: null,
        comments: null,
        heartBeatSt: null,
        isok: null,
        vanderlandPort: null,
        errorcode: null,
        errrfidlist: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("${column.dictType}").then(response => {
      this.dvcIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.dvcNoOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.dvcDescOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.dvcTpOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.dvcIpOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.revBeginOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.revEndOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.revProcOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.sendProcOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.sendTimeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.portOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.dvcStOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.createDateOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.updateDateOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.commentsOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.heartBeatStOptions = response.data;
    });
    this.getDicts("sys_job_status").then(response => {
      this.isokOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.vanderlandPortOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.errorcodeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.errrfidlistOptions = response.data;
    });
  },
  methods: {
    /** 查询PLC接口参数列表 */
    getList() {
      this.loading = true;
              listWcsDvc(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
        this.WcsDvcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // ID字典翻译
    dvcIdFormat(row, column) {
      return this.selectDictLabel(this.dvcIdOptions, row.dvcId);
    },
    // PLC编号字典翻译
    dvcNoFormat(row, column) {
      return this.selectDictLabel(this.dvcNoOptions, row.dvcNo);
    },
    // 厂商名称字典翻译
    dvcDescFormat(row, column) {
      return this.selectDictLabel(this.dvcDescOptions, row.dvcDesc);
    },
    // 接口程序名称字典翻译
    dvcTpFormat(row, column) {
      return this.selectDictLabel(this.dvcTpOptions, row.dvcTp);
    },
    // IP地址字典翻译
    dvcIpFormat(row, column) {
      return this.selectDictLabel(this.dvcIpOptions, row.dvcIp);
    },
    // 报文头字典翻译
    revBeginFormat(row, column) {
      return this.selectDictLabel(this.revBeginOptions, row.revBegin);
    },
    // 报文尾字典翻译
    revEndFormat(row, column) {
      return this.selectDictLabel(this.revEndOptions, row.revEnd);
    },
    // 接收存储过程字典翻译
    revProcFormat(row, column) {
      return this.selectDictLabel(this.revProcOptions, row.revProc);
    },
    // 发送存储过程字典翻译
    sendProcFormat(row, column) {
      return this.selectDictLabel(this.sendProcOptions, row.sendProc);
    },
    // 发送间隔字典翻译
    sendTimeFormat(row, column) {
      return this.selectDictLabel(this.sendTimeOptions, row.sendTime);
    },
    // 端口号字典翻译
    portFormat(row, column) {
      return this.selectDictLabel(this.portOptions, row.port);
    },
    // DVC_ST字典翻译
    dvcStFormat(row, column) {
      return this.selectDictLabel(this.dvcStOptions, row.dvcSt);
    },
    // 创建时间字典翻译
    createDateFormat(row, column) {
      return this.selectDictLabel(this.createDateOptions, row.createDate);
    },
    // 更新时间字典翻译
    updateDateFormat(row, column) {
      return this.selectDictLabel(this.updateDateOptions, row.updateDate);
    },
    // 备注字典翻译
    commentsFormat(row, column) {
      return this.selectDictLabel(this.commentsOptions, row.comments);
    },
    // HEART_BEAT_ST字典翻译
    heartBeatStFormat(row, column) {
      return this.selectDictLabel(this.heartBeatStOptions, row.heartBeatSt);
    },
    // 状态字典翻译
    isokFormat(row, column) {
      return this.selectDictLabel(this.isokOptions, row.isok);
    },
    // vanderland_port字典翻译
    vanderlandPortFormat(row, column) {
      return this.selectDictLabel(this.vanderlandPortOptions, row.vanderlandPort);
    },
    // errorCode字典翻译
    errorcodeFormat(row, column) {
      return this.selectDictLabel(this.errorcodeOptions, row.errorcode);
    },
    // errRfidList字典翻译
    errrfidlistFormat(row, column) {
      return this.selectDictLabel(this.errrfidlistOptions, row.errrfidlist);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dvcId: null,
        dvcNo: null,
        dvcDesc: null,
        dvcTp: null,
        dvcIp: null,
        revBegin: null,
        revEnd: null,
        revProc: null,
        sendProc: null,
        sendTime: null,
        port: null,
        dvcSt: null,
        createDate: null,
        updateDate: null,
        comments: null,
        heartBeatSt: null,
        isok: null,
        vanderlandPort: null,
        errorcode: null,
        errrfidlist: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dvcId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加PLC接口参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dvcId = row.dvcId || this.ids
      getWcsDvc(dvcId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改PLC接口参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dvcId != null) {
            updateWcsDvc(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addWcsDvc(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dvcIds = row.dvcId || this.ids;
      this.$confirm('是否确认删除PLC接口参数编号为"' + dvcIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWcsDvc(dvcIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有PLC接口参数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWcsDvc(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
